/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.view;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.BaseDialog;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;

import com.xuexiang.xui_lib.popup.ListConntainerDropdown;
import com.xuexiang.xui_lib.util.UntilTNT;
import com.xuexiang.xui_lib.util.ViewModel;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.provider.DialogSetterProvider;
import java.util.ArrayList;
import java.util.List;

/**
 * 显示框
 *
 * @since 2021-03-30
 */
public class DialogDropdownText extends DirectionalLayout implements ListConntainerDropdown {
    private Component component;
    private Context context;
    private DependentLayout Detlayout;
    private boolean isdix = true; //判断图片的指向方向
    private boolean istext = true;//判断是text还是TextField
    private ListContainer listContainer;
    private ViewModel viewModel;
    private PopupDialog popupDialog;
    private Text text_bx;
    private TextField field_text;
    private Image img_bx;
    private AnimatorValue animator;
    private String[] data = null;
    private List<UntilTNT> list;
    private DialogSetterProvider SetterProvider;
    public boolean isAnimation = true; //是否开启动画
    public boolean isclick = true; //是否可以点击
    public int PopubMode = LayoutAlignment.BOTTOM; //设置popub显示的位置

    public DialogDropdownText(Context context) {
        super(context);
        this.context = context;
        viewModel = new ViewModel(context);
        data = viewModel.getStringResource(ResourceTable.Strarray_drop_array);//获取json文件,Strarray_drop_content
        initview();
        AnimatorValue();
    }

    public DialogDropdownText(Context context, AttrSet attrSet) {
        super(context, attrSet);
        this.context = context;
        viewModel = new ViewModel(context);
        data = viewModel.getStringResource(ResourceTable.Strarray_drop_array);//获取json文件,Strarray_drop_content
        initview();
        AnimatorValue();
    }

    public DialogDropdownText(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        this.context = context;
        viewModel = new ViewModel(context);
        data = viewModel.getStringResource(ResourceTable.Strarray_drop_array);//获取json文件,Strarray_drop_content
        initview();
        AnimatorValue();
    }

    private void initview() {
        Component ct = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dropdown_dialog_text_layout, this, true);
        Detlayout = (DependentLayout) ct.findComponentById(ResourceTable.Id_Detlayout);

        text_bx = (Text) ct.findComponentById(ResourceTable.Id_text_bx);
        field_text = (TextField) ct.findComponentById(ResourceTable.Id_field_text);
        img_bx = (Image) ct.findComponentById(ResourceTable.Id_img_bx);

        Detlayout.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (isclick) {
                    if (isdix) {
                        isdix = false;
                        popupDialog = new PopupDialog(context, component);
                        if (isAnimation) {
                            animator.start();
                        }
                        popupDialog.setAutoClosable(true);
                        popupDialog.setCustomComponent(initComponent());
                        popupDialog.setMode(PopubMode);//PopubMode
                        img_bx.setPixelMap(ResourceTable.Media_icon_sjt);//向上的箭头
                        //  popupDialog.setSize(width, AttrHelper.vp2px(150, getContext()));
                        popupDialog.setDialogListener(new BaseDialog.DialogListener() {
                            //监听点击外部时消失的dialog
                            @Override
                            public boolean isTouchOutside() {
                                isdix = true;
                                img_bx.setPixelMap(ResourceTable.Media_icon_xjt);//箭头向下
                                return false;
                            }
                        });
                        popupDialog.show();
                    } else {
                        isdix = true;
                        img_bx.setPixelMap(ResourceTable.Media_icon_xjt);//
                    }
                } else {
                    //不可编辑设置背景颜色
                  //  setNoCilik();
                }
            }
        });
    }

    public Component initComponent() {
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_list_dialog_layout, null, false);
        listContainer = (ListContainer) component.findComponentById(ResourceTable.Id_listconid);
        //点击事件
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                UntilTNT untilTNT = (UntilTNT) listContainer.getItemProvider().getItem(i);
                //UntilTNT.SetToastDialog(context, untilTNT.getName());
                //设置text
                if (istext) { //true
                    text_bx.setText(untilTNT.getName());
                } else {
                    field_text.setText(untilTNT.getName());
                }

                isdix = true;
                img_bx.setPixelMap(ResourceTable.Media_icon_xjt);//
                popupDialog.hide();
            }
        });
        //   setList(ResourceTable.Strarray_drop_content);

        list = new ArrayList<>();
        for (int i = 0; i < data.length; i++) {
            UntilTNT untilTNT = new UntilTNT();
            untilTNT.setDisplay(false);
            untilTNT.setName(data[i]);
            list.add(untilTNT);
        }
        SetterProvider = new DialogSetterProvider(list, context);
        listContainer.setItemProvider(SetterProvider);
        this.component = component;
        return component;
    }

    /**
     *  设置显示的文本为第几个数据
     * @param a
     */
    public void  setDataArray(int a){
        text_bx.setText(data[a]);
    }
    /**
     * 设置整个背景的背景颜色
     *
     * @param element
     */
    public void setbackgroud(Element element) {
        Detlayout.setBackground(element);
    }

    /**
     * 设置是否可以点击状态切换
     * @param isck ture 可以点击
     */
    public void setNoCilik(boolean isck) {
        this.isclick=isck;
        Element touchedBackGround;
        if (isclick) {//可以点击
            touchedBackGround = ElementScatter.getInstance(context)
                    .parse(ResourceTable.Graphic_background_ability_xtoast);
        } else {

            touchedBackGround = ElementScatter.getInstance(context)
                    .parse(ResourceTable.Graphic_backfroud_search);
        }
        Detlayout.setBackground(touchedBackGround);
    }

    /**
     * 设置是否显示编辑框
     */
    public void settextfile() {
        text_bx.setVisibility(Component.HIDE);//text默认是显示的
        field_text.setVisibility(Component.VISIBLE);
        istext = false;//默认是ture
    }

    /**
     * 如果设置输入框的宽度 需要先调用settextfile
     * @param with 宽度
     */
    public void settexttypewith(int with){
        int a=AttrHelper.vp2px(with,context);
        //设置text
        if (istext) { //true
            text_bx.setWidth(a);
        } else {
            field_text.setWidth(a);
        }
    }
    /**
     * 需要传入的内部文件数组,会重新加载下拉控件
     *
     * @param json
     */
    public void setList(int json) {
        data = viewModel.getStringResource(json);//获取json文件,Strarray_drop_content
        if (list.isEmpty()) { //ture为空
            list.clear();
        }

        for (int i = 0; i < data.length; i++) {
            UntilTNT untilTNT = new UntilTNT();
            untilTNT.setDisplay(false);
            untilTNT.setName(data[i]);
            list.add(untilTNT);
        }
        SetterProvider.notifyDataChanged(); //刷新适配器
       /* SetterProvider SetterProvider = new SetterProvider(list, context);
        listContainer.setItemProvider(SetterProvider);*/

    }

    /**
     * 文本框
     *
     * @param content 传入显示的内容
     */
    public void setTextContent(String content) {
        // content = content.length()<0 ? data[0]:content;
        text_bx.setText(content);
    }

    //内部定义的自动动画
    private void AnimatorValue() {

        animator = new AnimatorValue();
        animator.setDuration(1000);//设置动画的持续时间
        animator.setDelay(100);//设置动画延迟
        // animator.setLoopedCount(1);//设置动画的重复时间 秒
        //设置动画师使用的曲线，默认情况下，该曲线是线性插值器。
        animator.setCurveType(Animator.CurveType.BOUNCE);

        animator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float value) {
                component.setContentPosition(0, 100 * value);

            }
        });
        animator.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {

            }

            @Override
            public void onStop(Animator animator) {
                component.setContentPosition(0, 0);
            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {

            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {

            }
        });
    }
}
